import 'package:ccshop/entites/GoodsEntity.dart';
import 'package:ccshop/eventbus/CartGoodEvent.dart';
import 'package:ccshop/eventbus/EventBus.dart';
import 'package:ccshop/utils/Constants.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class CartListView extends StatefulWidget {
  @override
  _CartListViewState createState() => _CartListViewState();
}

class _CartListViewState extends State<CartListView> {
  List<GoodsEntity> goodsList = List();
  List<String> selectList = List();
  double totalPrice = 0.00;

  _CartListViewState() {
    GoodsEntity goodsEntity = GoodsEntity(
      "images/test/test01.png",
      "波兰土豆进口【次日达】",
      "波兰原装进口 新鲜自然 醇香浓厚",
      "￥39.99",
      "￥45.00",
    );
    goodsList.add(goodsEntity);

    goodsEntity = GoodsEntity(
      "images/test/test02.png",
      "瑞士大白菜进口【今日达】",
      "波兰原装进口 清甜可口",
      "￥9.99",
      "￥25.00",
    );
    goodsList.add(goodsEntity);

    goodsEntity = GoodsEntity(
      "images/test/test03.png",
      "粉西红柿（番茄）500g/斤",
      "口味甘甜 圆润可口 皮薄肉多",
      "￥11.99",
      "￥35.00",
    );
    goodsList.add(goodsEntity);

    goodsEntity = GoodsEntity(
      "images/test/test04.png",
      "新鲜的猪肉 500g/斤",
      "每日新鲜 赶快来买",
      "￥25.99",
      "￥35.05",
    );
    goodsList.add(goodsEntity);

    goodsEntity = GoodsEntity(
      "images/test/test05.png",
      "刚出的竹笋",
      "大熊猫和人都喜欢吃的",
      "￥29.99",
      "￥45.15",
    );
    goodsList.add(goodsEntity);

    goodsEntity = GoodsEntity(
      "images/test/test06.png",
      "超新鲜蒜台 450g/斤",
      "甘甜可口 蒜台炒肉",
      "￥9.99",
      "￥18.99",
    );
    goodsList.add(goodsEntity);
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    bus.on(Constants.EVENT_ALL_SELECT, (arg) {
      if (arg) {
        // 全选
        selectList.clear();
        totalPrice = 0.00;
        setState(() {
          for (int i = 0; i < goodsList.length; i++) {
            selectList.add(goodsList[i].goodsSrc);
            totalPrice += double.parse(goodsList[i].goodsPrice.substring(1));
          }
          bus.emit(Constants.EVENT_ALL_SELECT_ITEM, CartGoodEvent(true, totalPrice, selectList.length));
        });
      } else {
        // 全不选
        setState(() {
          totalPrice = 0.00;
          selectList.clear();
          bus.emit(Constants.EVENT_ALL_SELECT_ITEM, CartGoodEvent(false, totalPrice, 0));
        });
      }
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    bus.off(Constants.EVENT_ALL_SELECT);
  }

  @override
  Widget build(BuildContext context) {
    return Expanded(
        child: Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10),
        color: Colors.white,
      ),
      padding: EdgeInsets.only(top: 20, bottom: 6),
      margin: EdgeInsets.all(10),
      child: ListView.separated(
        itemBuilder: (context, index) {
          return Container(
            margin: EdgeInsets.only(right: 10),
            child: Row(
              children: [
                Padding(
                  padding: EdgeInsets.only(left: 10),
                ),

                InkWell(
                  onTap: () {
                    setState(() {
                      if (selectList.contains(goodsList[index].goodsSrc)) {
                        selectList.remove(goodsList[index].goodsSrc);
                        totalPrice -= double.parse(
                            goodsList[index].goodsPrice.substring(1));
                      } else {
                        selectList.add(goodsList[index].goodsSrc);
                        totalPrice += double.parse(
                            goodsList[index].goodsPrice.substring(1));
                      }
                      if (selectList.length == goodsList.length) {
                        bus.emit(Constants.EVENT_ALL_SELECT_ITEM,
                            CartGoodEvent(true, totalPrice, selectList.length));
                      } else {
                        bus.emit(Constants.EVENT_ALL_SELECT_ITEM,
                            CartGoodEvent(false, totalPrice, selectList.length));
                      }
                    });
                  },
                  child: Icon(
                    Icons.check_circle,
                    color: selectList.contains(goodsList[index].goodsSrc)
                        ? Colors.red
                        : Colors.grey,
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(left: 6),
                ),

                Image.asset(
                  goodsList[index].goodsSrc,
                  width: 90,
                  height: 90,
                  fit: BoxFit.cover,
                ),
                Padding(padding: EdgeInsets.only(left: 10)),

                // 右边布局
                _HomeBottomRightView(goodsList[index]),
              ],
            ),
          );
        },
        separatorBuilder: (context, index) {
          return Divider(
            color: Colors.grey,
            height: 50,
            indent: 10,
          );
        },
        itemCount: goodsList.length,
        shrinkWrap: true,
      ),
    ));
  }
}

class _HomeBottomRightView extends StatelessWidget {
  final GoodsEntity goodsEntity;

  _HomeBottomRightView(this.goodsEntity);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => GoodsEntity("", "", "", "", ""),
      child: Consumer<GoodsEntity>(
        builder: (context, value, child) {
          return Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  goodsEntity.goodsName,
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                Text(
                  goodsEntity.goodsDesc,
                  style: TextStyle(fontSize: 10, color: Colors.grey),
                ),
                Padding(padding: EdgeInsets.only(top: 14, bottom: 14)),
                Row(
                  children: [
                    Expanded(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            goodsEntity.goodsPrice,
                            style: TextStyle(
                              fontSize: 20,
                              color: Colors.red,
                            ),
                          ),
                          Row(
                            children: [
                              InkWell(
                                onTap:() {
                                  context.read<GoodsEntity>().minus();
                                },
                                child: Image.asset(
                                  "images/shop_minus_icon.png",
                                  width: 22,
                                  height: 22,
                                ),
                              ),
                              Padding(
                                padding: EdgeInsets.only(left: 10),
                              ),
                              Text("${context.watch<GoodsEntity>().getGoodsCount}"),
                              Padding(
                                padding: EdgeInsets.only(left: 10),
                              ),
                              InkWell(
                                onTap:() {
                                  context.read<GoodsEntity>().add();
                                },
                                child: Image.asset(
                                  "images/shop_plus_icon.png",
                                  width: 20,
                                  height: 20,
                                ),
                              ),
                            ],
                          )
                        ],
                      ),
                    ),
                  ],
                )
              ],
            ),
          );
        },
      ),
    );
  }
}
